#
# Run experiments for diploma thesis (new, DPM kernels).
#

use strict;
use warnings;

my @folders = ('gen\results\qq', 'gen\results\pp', 'gen\results\p_', 'gen\results\q_', 	'gen\results\_p', 'gen\results\_q', 'gen\results\pq', 'gen\results\qp');
my $n = 0;
my @empty = ('') x 200;
my @quantitative = ('minkowski', 'dotproduct',
    'cosine', 'mahalanobis', 'kullback', 'bhattacharyya', 'hellinger', 'canberra',
    'tanimoto', 'divergence', 'intraclass', 'correlation', 'cohen', 'meehl', 'histogram', 'kullback', 
    'jeffrey', 'exponential', 'kagan',
    'unger',);
my @predicate = ('cooccurences', 'russel', 'hamming', 'matching', 'complement', 'fcm', 'kulczynski', 'pattern', 'browsing', 'koeppen', 'jaccard', 'braun', 'ecological', 'variance',
    'baulieu', 'kulczynski2', 'forbes', 'hamann', 'baulieu2', 'binary', 'rogers',
    'gower', 'steffensen', 'czekanowski', 'sorensen', 'sneath', 'anderberg', 'sorgenfrei',
    'driver', 'kulczynski3', 'retrieval', 'sneath2', 'mountford', 'mcconnaughey',
    'jones', 'maron', 'batagelj', 'tarwid', 'fager', 'stuart', 'koeppen2', 'hawkins',
    'benini', 'gilbert', 'baroni', 'modifiedgini', 'peirce', 'coefficientarithmetic',
    'gini', 'eyraud', 'goodman', 'peirce2', 'pearson', 'relative', 'goodman2', 'michael',
    'sneath2', 'yule', 'cole', 'loevinger', 'goodman3', 'maxwell', 'pearson2', 'baroni2',
    'sneath4', 'scott', 'yule2', 'digby', 'proportion', 'cohen2', 'fleiss', 'meansquare',
    'chisquare', 'goodman4', 'goodman5',);

my @generalization = ('dirac', 'gaussian', 'shepard', 'tenenbaum',
    'none', 'naive2');
	
my @modes = ('qp', 'qq', 'pp', 'pq', );
	
print "Percentage Correct Classifications;Mode;Taxonomic Measure;Index;Generalization Function;Index;Thematic Measure;Index;Viable;Percentage Correct L;Percentage Correct R;Command 1 (only once);Command 2;Command 3;Summary File\n";

foreach my $folder (@folders) {

	opendir D, "$folder" or die "Could not open dir: $!\n";
	my @files = grep(/pr-\d+/i, readdir D);
	foreach my $file (@files) {
		next unless $file =~ /pr-(\d+)-(\d)+-(\d+)-(\d+)-(\d+)/;
		my ($prec, $mode, $g, $lhs, $rhs,) = ($1, $2, $3, $4, $5);
		++$n;
		
		my $tbin = 'dpm.exe';
		my ($lwhich, $rwhich);
		if($folder =~ /qq/) {
			$lwhich = \@quantitative;
			$rwhich = \@quantitative;
		}
		elsif($folder =~ /_q/) {
			$lwhich = \@empty;
			$rwhich = \@quantitative;
			$tbin = 'rdpm.exe';
		}
		elsif($folder =~ /q_/) {
			$lwhich = \@quantitative;
			$rwhich = \@empty;
			$tbin = 'ldpm.exe';
		}
		elsif($folder =~ /pp/) {
			$lwhich = \@predicate;
			$rwhich = \@predicate;
		}
		elsif($folder =~ /_p/) {
			$lwhich = \@empty;
			$rwhich = \@predicate;
			$tbin = 'rdpm.exe';
		}
		elsif($folder =~ /p_/) {
			$lwhich = \@predicate;
			$rwhich = \@empty;
			$tbin = 'ldpm.exe';
		}
		elsif($folder =~ /qp/) {
			$mode = 0;
			$lwhich = \@quantitative;
			$rwhich = \@predicate;
		}
		elsif($folder =~ /pq/) {
			$mode = 3;
			$lwhich = \@predicate;
			$rwhich = \@quantitative;
		}
		else {
			die("Did not recognize $file by filename");
		}
		
		my $lname = $lwhich->[$lhs];
		my $rname = $rwhich->[$rhs];	
		my $viable = isViable($mode, $g, $lhs, $rhs, $prec, $folder);
		my $modename = $modes[$mode];
		
		my $commands = "dpm.exe features;$tbin train \"-t 4 -# 10000 -r $mode -g $g -s $lhs -d $rhs\"" . ";$tbin classify";
		print "$prec;$modename;" . $lname . ';' . $lhs . ';' . $generalization[$g] . ';' . $g . ';' . $rname . ';' . $rhs . ';' . $viable . ';' . $commands . ';' . $file . "\n";
		
	}
	closedir D;

}

sub isViable {

	my $mode = shift;
	my $g = shift;
	my $lhs = shift;
	my $rhs = shift;
	my $prec = shift;
	my $folder = shift;
	my ($lprec, $rprec) = (0, 0);
	my ($lname, $rname) = ('', '');
	my ($l, $r);
	
	if($folder =~ /_/) {
		return '0;;';
	}	
	if($folder =~ /pp/) {
		$l = 'gen\results\p_';
		$r = 'gen\results\_p';		
	}
	if($folder =~ /qq/) {
		$l = 'gen\results\q_';
		$r = 'gen\results\_q';	
	}
	if($folder =~ /pq/) {
		$l = 'gen\results\p_';
		$r = 'gen\results\_q';	
	}
	if($folder =~ /qp/) {
		$l = 'gen\results\q_';
		$r = 'gen\results\_p';
	}

	opendir LEFT, $l or die "Could not open dir: $!\n";
	opendir RIGHT, $r or die "Could not open dir: $!\n";	
	my @lfiles = grep(/pr-\d+-\d+-$g-$lhs-\d+?/i, readdir LEFT);
	if(scalar @lfiles == 1) {
		$lfiles[0] =~ /pr-(\d+)/;
		$lprec = $1;
	}
	my @rfiles = grep(/pr-\d+-\d+-$g-\d+?-$rhs/i, readdir RIGHT);
	if(scalar @rfiles == 1) {
		$rfiles[0] =~ /pr-(\d+)/;
		$rprec = $1;
	}
	
	return "0;$lprec;$rprec" if ($prec <= $lprec || $prec <= $rprec);
	return "1;$lprec;$rprec";
	
}
